//
//  CSGXBodyPageView.m
//  CarSalesMS_GCX
//
//  Created by Tony on 2016/10/21.
//  Copyright © 2016年 Tony. All rights reserved.
//

#import "CSGXBodyPageView.h"
#import "CSGXButton.h"


@interface CSGXBodyPageView ()

@property (nonatomic, strong) NSMutableArray<CSGXButton *> * btnArrM;

@property (nonatomic, strong) UIButton * btnSelected;
///图片数据源
@property (nonatomic, strong) NSDictionary *imgDict;


@end

@implementation CSGXBodyPageView

#pragma mark - Actions

- (void)btnTouchDown:(UIButton *)sender {
    
    if (sender.tag == _selectedIndex) {
        return;
    }
    
    [self cancelTouch];
    
    sender.selected = YES;
    self.btnSelected = sender;
    sender.backgroundColor = kColorButton;
    
}

- (void)btnTouchUpInside:(UIButton *)sender withEvent:(UIEvent *)event {
//    NSLog(@"touchUpInside");
    
    if (![sender gx_isTouchedPointInsideWithEvent:event]) {
        [self cancelTouch];
        NSLog(@"cancel---");
        return;
    }
        // 根据tag跳转到不同的控制器
    self.selectedIndex = sender.tag;
//    NSLog(@"--self.selectedIndex: %lu", self.selectedIndex);
    
    [self sendActionsForControlEvents:UIControlEventValueChanged];
}

- (void)btnTouchDragExit:(UIButton *)sender {
    NSLog(@"touchDragExit");
    [self cancelTouch];
}

- (void)cancelTouch {
    self.btnSelected.selected = NO;
    self.btnSelected.backgroundColor = [UIColor whiteColor];
    self.selectedIndex = -1;
}


#pragma mark - methods



#pragma mark - setupUI

- (void)setupElements {
    self.backgroundColor = [UIColor whiteColor];
    self.selectedIndex = -1;
    
//    NSArray * titleArr = @[@"1.销售出库", @"2.订单查询"];
//    NSArray * imgNameArr = @[@"carSale", @"query"];
    
    NSMutableArray<CSGXButton *> * btnArrM = [NSMutableArray array];
    
    // 循环创建按钮
    for (int i = 0; i < self.titleList.count; i++) {
//        NSLog(@"%@", self.imgDict);
        NSString *imgName = self.imgDict[self.titleList[i]];
        if (imgName == nil || imgName.isEmpty) {
            imgName = @"query";
        }
        CSGXButton * button = [CSGXButton gxButtonWithTitle:self.titleList[i] fontSize:15 image:imgName];
        button.tag = i;

        // 
        [button addTarget:self action:@selector(aInside:withEvent:) forControlEvents:UIControlEventTouchDragInside];
        
        [button addTarget:self action:@selector(btnTouchDown:) forControlEvents:UIControlEventTouchDown];
        [button addTarget:self action:@selector(btnTouchUpInside:withEvent:) forControlEvents:UIControlEventTouchUpInside];
        [button addTarget:self action:@selector(btnTouchDragExit:) forControlEvents:UIControlEventTouchDragExit];
        
        [btnArrM addObject:button];
        [self addSubview:button];
        
//        button.backgroundColor = [UIColor gx_randomColor];
    }
    self.btnArrM = btnArrM;
    
}

- (void)aInside:(UIButton *)sender withEvent:(UIEvent *)event {
    if (![sender gx_isTouchedPointInsideWithEvent:event]) {
        [self cancelTouch];
        return;
    }
}

- (void)layoutSubviews {
    // 给按钮布局
    [self gx_nineBoxAlgorithm:_btnArrM marginTop:5 itemMargin:5 itemWidth:0 itemHeight:100 columCount:3];
}

#pragma mark - setter & getter

- (void)setTitleList:(NSArray<NSString *> *)titleList {
    _titleList = titleList;
    
    [self setupElements];
}

- (NSDictionary *)imgDict {
    if (!_imgDict) {
        _imgDict = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"HomeBodyElement.plist" ofType:nil]][0];
    }
//    NSLog(@"%@", _imgDict);
    return _imgDict;
}

@end
